package com.EAIPClub.userauth.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.EAIPClub.userauth.service.RegisterService;
import com.api.API.UserBaseInfoManager.UserBaseInfoDataManagerAPI;
import com.api.API.UserDataManager.UserDataManagerAPI;
import com.api.ConstantPool;
import com.api.POJO.UserAuth.RegisterAccount;
import com.api.POJO.UserAuth.RegisterReturnMsg;
import com.api.POJO.UserBaseInfoManager.BaseInfo;
import com.api.POJO.UserDataManager.Data.User;
import com.api.POJO.UserDataManager.Data.UserGroup;
import com.api.POJO.UserDataManager.Data.UserPrivileges;
import com.api.POJO.UserDataManager.Result.UserDataReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description :用户注册业务逻辑层实现
 * @Author :崔昌赫
 * @Date : 2021/10/21
 */
@Service
@Slf4j
public class RegisterServiceImpl implements RegisterService {

    private UserDataManagerAPI userDataManagerAPI;

    @Autowired
    public void setUserDataManagerAPI(UserDataManagerAPI userDataManagerAPI) {
        this.userDataManagerAPI = userDataManagerAPI;
    }

    private UserBaseInfoDataManagerAPI userBaseInfoDataManagerAPI;

    @Autowired
    public void setUserBaseInfoManagerAPI(UserBaseInfoDataManagerAPI userBaseInfoDataManagerAPI) {
        this.userBaseInfoDataManagerAPI = userBaseInfoDataManagerAPI;
    }

//    private UserAuthDataManagerAPI userAuthDataManagerAPI;
//
//    @Autowired
//    public void setUserAuthDataManagerAPI(UserAuthDataManagerAPI userAuthDataManagerAPI) {
//        this.userAuthDataManagerAPI = userAuthDataManagerAPI;
//    }

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    @Transactional
    public RegisterReturnMsg register(RegisterAccount account) {
        RegisterReturnMsg returnMsg = new RegisterReturnMsg();
        List<String> Msg=new ArrayList<>();
        UserDataReturnMsg userAllInfoByEmail = userDataManagerAPI.findUserAllInfoByEmail(account.getEmail());
        if(userAllInfoByEmail.getStatus().equals(ConstantPool.OK)){
            log.warn("查询到该邮箱已经存在!信息为:{}",userAllInfoByEmail);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("该邮箱已存在!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        String date = simpleDateFormat.format(new Date());
        String uid = SecureUtil.md5(date+account.getEmail());
        account.getBaseInfo().setUid(uid);
        addNewUserBaseData(uid,account.getEmail(),account.getPassword());
        addNewUserPrivilegesData(uid,1,1,1);
        addNewUserGroupData(uid,1);
        addNewBaseInfo(account.getBaseInfo());
        log.info("注册完毕,用户所有信息为:uid:{},data:{},baseinfo:{}",uid,userDataManagerAPI.findUserAllInfoByUid(uid), userBaseInfoDataManagerAPI.findBaseInfoByUid(uid));
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Async
    @Transactional
    public void addNewUserBaseData(String uid,String email,String password){
        log.info("开始添加用户基本信息到数据库.uid:{},email:{},password:{}",uid,email,password);
        userDataManagerAPI.addUserBaseInfo(new User(null,uid,email,password));
    }

    @Async
    @Transactional
    public void addNewUserPrivilegesData(String uid,Integer codeIde,Integer codeShell,Integer codeDesktop){
        log.info("开始添加用户权限信息到数据库.uid:{},codeIde:{},codeShell:{},codeDesktop:{}",uid,codeIde,codeShell,codeDesktop);
        userDataManagerAPI.addUserPrivileges(new UserPrivileges(uid,codeIde,codeShell,codeDesktop));
    }

    @Async
    @Transactional
    public void addNewUserGroupData(String uid,Integer groupId){
        log.info("开始添加用户所属用户组信息到数据库.uid:{},groupId:{}",uid,groupId);
        userDataManagerAPI.addUserGroup(new UserGroup(uid,groupId));
    }

    @Async
    @Transactional
    public void addNewBaseInfo(BaseInfo baseInfo){
        log.info("开始添加用户基本信息:baseInfo:{}",baseInfo);
        userBaseInfoDataManagerAPI.addNewBaseInfo(baseInfo);
    }
}
